package com.shardingsphere.shardingsphere.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shardingsphere.shardingsphere.dao.ProjectMapper;
import com.shardingsphere.shardingsphere.dao.ProjectUserMapper;
import com.shardingsphere.shardingsphere.dao.UserMapper;
import com.shardingsphere.shardingsphere.domain.Project;
import com.shardingsphere.shardingsphere.domain.ProjectUser;
import com.shardingsphere.shardingsphere.service.ProjectService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

	@Autowired
	SqlSessionFactory sqlSessionFactory;

	@Autowired
	ProjectUserMapper projectUserMapper;

	@Override
	public boolean save(Project entity) {
		return super.save(entity);
	}

	@Override
	public List<Project> getProjectList() {

		QueryWrapper<Project> wrapper = new QueryWrapper();
		wrapper.lambda().getSqlSelect();
		wrapper.orderByDesc("create_time");
		return baseMapper.selectList(wrapper);

	}

	@Override
	public Project getProjectById(String id) {
		Map map=new HashMap();
		map.put("id",id);
		LocalDateTime date= LocalDateTime.parse("2018-09-01 06:11:14", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		ZoneId zone = ZoneId.systemDefault();
		//map.put("create_time",new Timestamp(date.atZone(zone).toInstant().toEpochMilli()));
		map.put("create_time","2018-09-01 06:11:14");
		Project project=sqlSessionFactory.openSession(true).selectOne("com.shardingsphere.shardingsphere.dao.ProjectMapper.selectProject",map);
//		QueryWrapper<Project> wrapper = new QueryWrapper();
//		wrapper.lambda().getSqlSelect();
//		wrapper.eq("id",id);
//		wrapper.eq("project_code","HH-2019-0001");
//
//		//wrapper.eq("create_time",new Timestamp(date.atZone(zone).toInstant().toEpochMilli()));
//
//
//		wrapper.eq("create_time","2018-09-01 06:11:14");
//		Project project=baseMapper.selectOne(wrapper);
		return project;
	}

	@Override
	public void addProjectUser(ProjectUser projectUser) {
		//sqlSessionFactory.openSession(true).insert("com.shardingsphere.shardingsphere.dao.ProjectUserMapper.insert",projectUser);
		int result=projectUserMapper.insert(projectUser);
		System.out.println("插入子项目行"+result);
	}

	@Override
	public ProjectUser getProjectUser(String id) {



		return projectUserMapper.selectByPrimaryKey(id);
	}

	public static void main(String[] args) {
		//LocalDateTime date= LocalDateTime.parse("2018-09-01 06:11:14", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		String date="2018-09-01 06:11:14";
		Date timestamp=Timestamp.valueOf(date);
		System.out.println(timestamp);



		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format.setLenient(false);
		//要转换字符串 str_test
		String str_test ="2018-09-01 06:11:14";
		try {
			Timestamp ts = new Timestamp(format.parse(str_test).getTime());
			System.out.println(ts);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
